#include "msgdeal.h"
#include <QCryptographicHash>
#include <QDebug>
msgdeal::msgdeal()
{

}
QString msgdeal::senddata(QString data)
{
int i;
qDebug()<<"明文长度"<<data.length();
//MD5
QString md5rslt;
md5rslt=MD5encryption(data);   //MD5
qDebug()<<"hash长度"<<md5rslt.length();
//对hash值进行RSA签名
QByteArray ba=md5rslt.toLocal8Bit();//QString 转字符数组
char *plain=ba.data();
int l=md5rslt.length();
for(i=0;i<l;i++){
    a.plaintext[i]=plain[i];
}
a.Initialize();
a.RSA_Signature();
qDebug()<<"\n 签名完成 \n";
//拼接
QString tmp;
tmp=data+QString::fromLocal8Bit((char*)a.Ciphertext);
//AES
QString key1=md5rslt.mid(0,16); //AES密钥取hash值
unsigned char* key;
key=(unsigned char*)qstrdup(key1.toLocal8Bit().data());
AES aes(key);
aes.input=tmp;
aes.Initialize();
aes.aes_encrypt();
tmp=aes.output;
qDebug()<<"AES 密文长度"<<tmp.length();
//RSA加密AES公钥
ba=key1.toLocal8Bit();//QString 转字符数组
char *plain2=ba.data();
l=key1.length();
for(i=0;i<l;i++){
    b.plaintext[i]=plain2[i];
}
b.Initialize();
b.RSA_Encrypt();
qDebug()<<"aes公钥加密完成 \n";
qDebug()<<"AES 公钥加密后长度"<<l;
//拼接
QString rslt=tmp+QString::fromLocal8Bit((char*)b.Ciphertext);
qDebug()<<"结果长度"<<rslt.length();
return rslt;

}

QString msgdeal::recvdata(QByteArray data)
{

}
QString msgdeal::MD5encryption(QString s)
{
    QString rslt;
    rslt=QCryptographicHash::hash(s.toLatin1(),QCryptographicHash::Md5).toHex();
    return rslt;
}
